package com.cneiu.ppms.web.service;

import java.util.Map;

import net.sf.json.JSONObject;

import com.cneiu.ppms.web.base.BaseService;
import com.cneiu.ppms.web.model.PowerPlantDocModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
/**
 * 电厂附件业务
 * @author zhaokf
 * @date 2013-04-22
 */
public class PowerPlantDocService extends BaseService
{
	/**
	 * 分页列出电厂附件
	 * @param page
	 * @param limit
	 * @param search
	 * @return
	 */
	public static Page<PowerPlantDocModel> list(Map<String,String> params){
		JSONObject search = JSONObject.fromObject(params.get("search"));
		int limit = 0;
		int page = 1;
		try{
			limit = params.get("limit")==null?10:Integer.parseInt(params.get("limit").toString());
			page = params.get("page")==null?1:Integer.parseInt(params.get("page").toString());
		}catch (Exception e){
			limit = 10;
			page = 1;
		}
		StringBuffer where = new StringBuffer();;
		try{
			if (search.get("doc_id") != null){
				where.append(" AND doc_id LIKE '%" + search.get("doc_id") + "%'");
			}
			if (search.get("doc_name") != null){
				where.append(" AND doc_name LIKE '%" + search.get("doc_name") + "%'");
			}
		}catch (Exception e){
		}
		Page<PowerPlantDocModel> PPD = null;

		String select_sql = "SELECT * ";
		String query_sql = "FROM C_PP_DOC WHERE 1=1 " + where;
		try{
			PPD = PowerPlantDocModel.dao.paginate(page, limit, select_sql, query_sql);
		}catch (Exception e){
			setError(e.getMessage());
		}
		return PPD;
	}


	/**
	 * 根据ID查询电厂附件
	 * @param params
	 * @return
	 */
	public static PowerPlantDocModel get(Map<String,String> params){
		String no = params.get("doc_id")==null?"":params.get("doc_id").toString();
		PowerPlantDocModel ppd = PowerPlantDocModel.dao.findFirst("SELECT * FROM C_PP_DOC WHERE doc_id=?", no);
		return ppd;
	}

	/**
	 * 增加电厂附件信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isAdd(Map<String,String> params){
		Record record = new Record();
		record.set("doc_id", Db.queryBigDecimal("select c_pp_doc_seq.nextval from dual").intValue());
		record.set("doc_pp_id", params.get("doc_pp_id").toString());
		record.set("doc_code", params.get("doc_code").toString());
		record.set("doc_type", params.get("doc_type").toString());
		record.set("doc_name", params.get("doc_name").toString());
		boolean isupadded = Db.save("C_PP_DOC", "doc_id",record);
		if(isupadded){
			return true;
		}
		return setError("保存电厂附件信息失败！");
		
	}

	/**
	 * 修改电厂附件信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isUpdate(Map<String,String> params){
		Record record = new Record();
		record.set("doc_id", params.get("doc_id").toString());
		record.set("doc_pp_id", params.get("doc_pp_id").toString());
		record.set("doc_code", params.get("doc_code").toString());
		record.set("doc_type", params.get("doc_type").toString());
		record.set("doc_name", params.get("doc_name").toString());
		boolean isupadded = Db.save("C_PP_DOC", "doc_id",record);
		if(isupadded){
			return true;
		}
		return setError("您访问的电厂附件不存在！");
	}

	/**
	 * 删除电厂附件信息
	 * @param no
	 * @return
	 */
	public static boolean isDelete(Map<String,String> params){
		String no = params.get("doc_id")==null?"":params.get("doc_id").toString();
		boolean deleted = Db.deleteById("C_PP_DOC", "doc_id", no);
		if (deleted){
			return true;
		}
		return setError("您访问的电厂附件不存在！");
	}
}


